package LinkedList;

public class Test {

    //相交链表
    public SingleList.ListNode getIntersectionNode(SingleList.ListNode headA, SingleList.ListNode headB) {
        if(headA == null && headB == null) return null;
        //1.先求两个链表的长度
        int lenA = 0;
        int lenB = 0;
        SingleList.ListNode L = headA;//假定L指向的链表是长的，S指向地链表是短的
        SingleList.ListNode S = headB;
        while(L != null) {
            lenA++;
            L = L.next;
        }
        while(S != null) {
            lenB++;
            S = S.next;
        }
        int len = lenA - lenB;
        if(len < 0) {//len为负数说明S是更长的链表
            L =headB;
            S = headA;//这样保证L指向的链表就是更长的链表
            len = lenB - lenA;//同时保证len一定是正数
        }

        for (int i = 0; i < len && L != null; i++) {//让长的链表先走差值步
            L = L.next;
        }

        SingleList.ListNode intersect = null;
        while(L != null && S !=null) {//同时走
            if(L.next == S.next) {
                intersect = L;
            }
            L = L.next;
            S = S.next;
        }
        if(intersect == null) {
            return null;
        }
        return intersect;
    }

    private static void createIntersection(SingleList.ListNode headA, SingleList.ListNode headB) {
        headB.next.next = headA.next.next;
    }
    public static void main(String[] args) {
        SingleList singleList = new SingleList();
        singleList.createList();

        singleList.display();
        //System.out.println(singleList.size());
        //System.out.println(singleList.contains(12));
        /*singleList.addFirst(200);
        singleList.addLast(45);
        singleList.insert(3,599);
        singleList.display();//200 12 25 599 88 47 32 45*/
        //singleList.delete(88);
        //singleList.display();
        //singleList.clear();
        //singleList.insert(2, 188);
        //singleList.insert(1, 88);
        //singleList.insert(3, 88);
        //singleList.display();
        //singleList.delete(88);
        //singleList.deleteAll(88);
        //singleList.display();
        // singleList.clear();
        /*System.out.println("=============");
        singleList.reverse(singleList);
        singleList.display();*/
        SingleList singleList1 = new SingleList();
        singleList1.addLast(12);
        singleList1.addLast(21);
        singleList1.addLast(88);
        singleList1.addLast(10);
        singleList1.addLast(32);

        SingleList singleList2 = new SingleList();
        singleList2.addLast(56);
        singleList2.addLast(87);
        singleList2.addLast(10);
        singleList2.addLast(89);

        createIntersection(singleList1.head, singleList2.head);
        System.out.println("====================");
        System.out.println(singleList2.isCircle());
    }
}
